package com.selvn.encryptnote;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class ChangePassword extends Activity
{
    private EditText passET;
    private int inputTimes;
    private String inputPass = "";
    
    //private Config myConfig;  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        passET = (EditText) findViewById(R.id.Pass);
        inputTimes = 0;
        /*
        Button confirmButton = (Button) findViewById(R.id.Confirm);
        
        confirmButton.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View view) 
                {
                    setResult(RESULT_OK);
                    finish();
                }
            }
        );
        */
    }

    public void LoginButtonClick( View v )
    {
        String passString = passET.getText().toString();
        switch (v.getId() )
        {
        case R.id.Button00:
            passET.setText(passString+="0");
            break;
        case R.id.Button01:
            passET.setText(passString+="1");
            break;
        case R.id.Button02:
            passET.setText(passString+="2");
            break;
        case R.id.Button03:
            passET.setText(passString+="3");
            break;
        case R.id.Button04:
            passET.setText(passString+="4");
            break;
        case R.id.Button05:
            passET.setText(passString+="5");
            break;
        case R.id.Button06:
            passET.setText(passString+="6");
            break;
        case R.id.Button07:
            passET.setText(passString+="7");
            break;
        case R.id.Button08:
            passET.setText(passString+="8");
            break;
        case R.id.Button09:
            passET.setText(passString+="9");
            break;
            /*
        case R.id.Reset:
            passET.setText("");
            break;
            */
        case R.id.Confirm:
            if( inputTimes == 0 )
            {
                inputTimes = 1;
                inputPass = passString;
                Toast.makeText(this, R.string.confirmInput, Toast.LENGTH_LONG).show();
                passET.setText("");
            }
            else if( inputTimes == 1 && inputPass.equals(passString))
            {
                inputTimes = 0;
                //myConfig.password = passString;;;
                if( SavePassword(passString) )
                {
                    setResult(RESULT_OK);
                }
                finish();
            }
            else
            {
                inputTimes = 0;
                Toast.makeText(this, R.string.confirmInputError, Toast.LENGTH_LONG).show();
                passET.setText("");
            }
            break;
        }
    }
    public boolean SavePassword( String password )
    {
        SharedPreferences settings = getSharedPreferences(Config.SETTINGS,Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(Config.SETTINGS, password);
        editor.commit();
        return true;
    }
}
